﻿using Emoney.UserTraining.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Emoney.UserTraining.Cache;
using Emoney.UserTraining.Model;

namespace Emoney.UserTraining.Web.Controllers
{
    public class GuangGaoController : Controller
    {

        //
        // GET: /GuangGao/
        public ActionResult Jupm()
        {
            string str = Request.QueryString.ToString();
            string type = Request["type"];
            string url = "";
            if (!string.IsNullOrEmpty(type))
            {
                switch (type)
                {
                    case "0":
                       // DownLoad();
                        //return View("DownLoad");
                        url = "http://ds.emoney.cn/train/GuangGao/DownLoad?"+str;
                        //url = "http://192.168.8.102:8118/guanggao/download?" + str;
                        break;
                    case "1":
                        //Close();
                        //return View("Close");
                        url = "http://ds.emoney.cn/train/GuangGao/Close?" + str;
                        break;
                }
            }
            return Redirect(url);
        }

        GetUserName.UserLoginInfo GetUser()
        {
            GetUserName.access access = new GetUserName.access();
            return access.GetUserInfo(Request.QueryString.ToString(), "127.0.0.1", 0);
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DownLoad()
        {
            GuangGaoBLL bll = new GuangGaoBLL();
            var c = GetUser();
            string clienttype = Request["clienttype"];
            string IsOutOfDate=Request["IsOutOfDate"];//1过期/0未过期
            GuangGaoEntity v= new GuangGaoEntity();
            if (c.UserName != null && c.UserName != "")
            {
                if (c.UserName.ToUpper().StartsWith("1YK"))
                {
                    clienttype = "1YK";
                    v = bll.GetByPID(clienttype, "1", c.Pid, Convert.ToInt32(IsOutOfDate));
                }
                else if (c.UserName.ToUpper().StartsWith("DS"))
                {
                    clienttype = "3";
                    c.Pid = "211";
                    IsOutOfDate = "0";
                    v = bll.GetByPID(clienttype, "1", c.Pid, Convert.ToInt32(IsOutOfDate));
                }
                else if (clienttype == "3" || clienttype == "15" && IsOutOfDate == "1")
                {
                    if (!string.IsNullOrEmpty(GetPid(c.UserName)))
                    {
                        c.Pid = GetPid(c.UserName);
                        v = bll.GetByPID("1", c.Pid, Convert.ToInt32(IsOutOfDate));
                    }
                }
                else {
                    v = bll.GetByPID(clienttype, "1", c.Pid, Convert.ToInt32(IsOutOfDate));
                }
            }
            else
            {
                v = bll.GetByPID(clienttype, "1", c.Pid, Convert.ToInt32(IsOutOfDate));
            }
          
            if (v != null)
            {
                ViewData["content"] = v.ggcontent;
                ViewData["sc"] = v.sc;
            }
            else
            {
                ViewData["content"] = "662057";
                ViewData["sc"] = 0;
            }
            ViewData["clienttype"] = clienttype;
            ViewData["lst"] = v;
            return View();
        }

        public string GetPid(string username)
        {
            //string key = "Emoney.UserTraining.Web.Controllers.EM_Get_PID_KFPID" + username;
            //News2CacheTools cache = new News2CacheTools();
            //object obj = cache.Get<object>(key);
            string pidstr = "";
            //if (obj != null)
            //{
            //    pidstr = (string)obj;
            //}
            //else {
            //    try
            //    {

                    emoneybusiness.emoneybusiness sv = new emoneybusiness.emoneybusiness();
                    int pid = sv.EM_Get_PID_KFPID(username);
                    pidstr = pid.ToString();
                    //cache.Add(key, pidstr, 1, 0, 0);
                //}catch(Exception e)
                //{
                //pidstr="";
                //}
            //}
            if (pidstr != "202100000")
            {
                if (!string.IsNullOrEmpty(pidstr))
                {
                    pidstr = pidstr.Substring(0, 3);
                }
            }
            return pidstr;

        }
        public ActionResult Close()
        {
            GuangGaoBLL bll = new GuangGaoBLL();
            var c = GetUser();
            string clienttype = Request["clienttype"];
            string IsOutOfDate = Request["IsOutOfDate"];//1过期/0未过期
            //if (c.UserName != null && c.UserName != "")
            //{
            //    if (c.UserName.ToUpper().StartsWith("1YK"))
            //    {
            //        clienttype = "1YK";
            //    }
            //    else if (c.UserName.ToUpper().StartsWith("DS"))
            //    {
            //        clienttype = "3";
            //        c.Pid = "211";
            //        IsOutOfDate = "0";
            //    }
            //    else if (clienttype == "3" || clienttype == "15")
            //    {
            //        if (!string.IsNullOrEmpty(GetPid(c.UserName)))
            //        {
            //            c.Pid = GetPid(c.UserName);
            //        }
            //    }
            //}
            //var v = bll.GetByPID(clienttype, "0", c.Pid, Convert.ToInt32(IsOutOfDate));
            GuangGaoEntity v = new GuangGaoEntity();
            if (c.UserName != null && c.UserName != "")
            {
                if (c.UserName.ToUpper().StartsWith("1YK"))
                {
                    clienttype = "1YK";
                    v = bll.GetByPID(clienttype, "0", c.Pid, Convert.ToInt32(IsOutOfDate));
                }
                else if (c.UserName.ToUpper().StartsWith("DS"))
                {
                    clienttype = "3";
                    c.Pid = "211";
                    IsOutOfDate = "0";
                    v = bll.GetByPID(clienttype, "0", c.Pid, Convert.ToInt32(IsOutOfDate));
                }
                else if (clienttype == "3" || clienttype == "15" && IsOutOfDate == "1")
                {
                    if (!string.IsNullOrEmpty(GetPid(c.UserName)))
                    {
                        c.Pid = GetPid(c.UserName);
                        v = bll.GetByPID("0", c.Pid, Convert.ToInt32(IsOutOfDate));
                    }
                }
                else
                {
                    v = bll.GetByPID(clienttype, "0", c.Pid, Convert.ToInt32(IsOutOfDate));
                }
            }
            else
            {
                v = bll.GetByPID(clienttype, "0", c.Pid, Convert.ToInt32(IsOutOfDate));
            }
            if (v != null)
            {
                ViewData["content"] = v.ggcontent;
                ViewData["sc"] = v.sc;
                ViewData["isshow"] = v.isshow;
            }
            else
            {
                ViewData["content"] = "";
                ViewData["sc"] = 0;
                ViewData["isshow"] = 0;
            }
            ViewData["lst"] = v;
            return View();
        }

    }
}
